<?php
/**
* Http response container class for Dj kit
*
* @link http://www.django-fr.org/documentation/request_response
* @package dj
* @subpackage http
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/


class DjHttpResponse {

    /**
     * constructor
     * @param string $content response body
     * @param string $mimeType
     * @param int $status HTTP response status code
     *
     */
    function __construct($content=null,$mimeType=null,$status=200,$contentType='text/html') {
        $this->write($content);
        $this->mime=$mimeType;
        $this->status_code=$status;
        // $this->
    }

    /**
     * sets a cookie
     **
    public function set_cookie($key, $value='', $max_age=null, $expires=null, $path='/', $domain=null, $secure=null) {

    }/**/

    /**
     * deletes a cookie
     * /
    public function delete_cookie($key, $path='/', $domain=null) {

    }/**/

    /**
     * sets an header
     * @param string $header key
     * @param string $value
     * @return bool
     */
    public function __set($header, $value) {
        $this->headers[$header]=$value;
        return true;
    }

    /**
     * gets an header
     * @param string $header
     * @return string
     */
    public function __get($header) {
        if (!isset($this->headers[$header]))
            return null;
        return $this->headers[$header];
    }

    /**
     * tells if header is defined
     * @param string $header
     * @return bool
     */
    public function hasHeader($header) {
        return isset($this->headers[$header]);
    }

    /**
     * appends some content
     * @param string $content
     * @return bool
     * @access public
     */
    function write($content) {
        if (!is_string($content))
            return false;
        $this->content.=$content;
        return true;
    }

    /**
     * empties content
     * @return bool
     */
    public function flush() {
        $this->content=null;
        return true;
    }

    /**
     * sends response to client
     * @return bool
     */
    public function send() {
        // sending status line header
        header($this->_statusLine());
        // other headers
        foreach ($this->headers as $key => $value)
            header("$key: $value",true);
        echo $this->content;
        return true;
    }

    /**
     * @var int http status code
     */
    public $status_code=200;

    /**
     * @var string mime type
     */
    public $mime;

    /**
     * @var string body content
     */
    public $content;
    /**
     * @var array(string) response headers key => value
     */
    protected $headers=array();

    /**
     * @var array cookies to be set
     */
    protected $cookies=array();

    /**
     * create status line header
     * @return string
     */
    protected function _statusLine() {
        $line='HTTP/1.1 '.$this->status_code;
        switch ($this->status_code) {
            case 200:
                return $line.' OK';
            case 204:
                return $line.' No Content';
            case 401:
                return $line.' Forbidden';
            case 403:
                return $line.' Forbidden';
            case 404:
                return $line.' Not Found';
            case 500:
                return $line.' Internal Server Error';
            case 501:
                return $line.' Not Implemented';
        }
    }

    var $_status_reasons=array(
        '100' => 'Continue'
        , '101' => 'Switching Protocols'
        , '200'  => 'OK'
        , '201'  => 'Created'
        , '202'  => 'Accepted'
        , '203'  => 'Non-Authoritative Information'
        , '204'  => 'No Content'
        , '205'  => 'Reset Content'
        , '206'  => 'Partial Content'
        , '300'  => 'Multiple Choices'
        , '301'  => 'Moved Permanently'
        , '302'  => 'Found'
        , '303'  => 'See Other'
        , '304'  => 'Not Modified'
        , '305'  => 'Use Proxy'
        , '307'  => 'Temporary Redirect'
        , '400'  => 'Bad Request'
        , '401'  => 'Unauthorized'
        , '402'  => 'Payment Required'
        , '403'  => 'Forbidden'
        , '404'  => 'Not Found'
        , '405'  => 'Method Not Allowed'
        , '406'  => 'Not Acceptable'
        , '407'  => 'Proxy Authentication Required'
        , '408'  => 'Request Time-out'
        , '409'  => 'Conflict'
        , '410'  => 'Gone'
        , '411'  => 'Length Required'
        , '412'  => 'Precondition Failed'
        , '413'  => 'Request Entity Too Large'
        , '414'  => 'Request-URI Too Large'
        , '415'  => 'Unsupported Media Type'
        , '416'  => 'Requested range not satisfiable'
        , '417'  => 'Expectation Failed'
        , '500'  => 'Internal Server Error'
        , '501'  => 'Not Implemented'
        , '502'  => 'Bad Gateway'
        , '503'  => 'Service Unavailable'
        , '504'  => 'Gateway Time-out'
        , '505'  => 'HTTP Version not supported');
}

class DjHttpResponseNotFound extends DjHttpResponse {

    public $status_code=404;

    public $content='<html>
    <head>
        <title>Page not found</title>
    </head>
    <body>
        <h1>Page not found</h1>
        <p>This is an error 404</p>
    </body>
</html>';
}

class DjHttpResponseServerError extends DjHttpResponse {
    public $status_code=500;

    public $content='<html>
    <head>
        <title>Ooops</title>
    </head>
    <body>
        <h1>Ooops</h1>
        <p>This is an error 500</p>
    </body>
</html>';
}
/**
 *HttpResponse subclasses

Django includes a number of HttpResponse subclasses that handle different types of HTTP responses. Like HttpResponse, these subclasses live in django.http.

HttpResponseRedirect
    The constructor takes a single argument — the path to redirect to. This can be a fully qualified URL (e.g. 'http://www.yahoo.com/search/') or an absolute URL with no domain (e.g. '/search/'). Note that this returns an HTTP status code 302.
HttpResponsePermanentRedirect
    Like HttpResponseRedirect, but it returns a permanent redirect (HTTP status code 301) instead of a “found” redirect (status code 302).
HttpResponseNotModified
    The constructor doesn’t take any arguments. Use this to designate that a page hasn’t been modified since the user’s last request.
HttpResponseBadRequest
    New in Django development version. Acts just like HttpResponse but uses a 400 status code.
HttpResponseNotFound
    Acts just like HttpResponse but uses a 404 status code.
HttpResponseForbidden
    Acts just like HttpResponse but uses a 403 status code.
HttpResponseNotAllowed
    Like HttpResponse, but uses a 405 status code. Takes a single, required argument: a list of permitted methods (e.g. ['GET', 'POST']).
HttpResponseGone
    Acts just like HttpResponse but uses a 410 status code.
HttpResponseServerError
    Acts just like HttpResponse but uses a 500 status code.


 */